package com.server.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.server.pojo.Forums;
import com.server.service.ForumsService;
import com.server.utils.ResponseMsg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 用户论坛帖子表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 09:29:27
 */
@RestController
@RequestMapping("/forums")
public class ForumsController {

    @Autowired
    private ForumsService forumsService;
    //查找所有用户论坛帖子信息
    @PostMapping("/findAll")
    public String findAll(@RequestBody Map<String,Object> param) {
       Integer pageNum = Integer.parseInt(param.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(param.get("pageSize").toString());
        QueryWrapper<Forums> queryWrapper = new QueryWrapper<>();
        IPage<Forums> page = new Page<>(pageNum, pageSize);
        return JSON.toJSONString(forumsService.page(page, queryWrapper));
    }
    //根据关键字模糊查询帖子
    @PostMapping("/findByKey")
    public String findByKey(@RequestBody Map<String,Object> param) {
        Integer pageNum = Integer.parseInt(param.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(param.get("pageSize").toString());
        String key = param.get("key").toString();
        String keyword = "%" + key + "%";
        QueryWrapper<Forums> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        IPage<Forums> page = new Page<>(pageNum, pageSize);
        return JSON.toJSONString(forumsService.page(page, queryWrapper));
    }
    //根据postId删除单条记录
    @PostMapping("/deleteById")
    public String deleteById(@RequestBody Map<String,Object> param) {
        Integer postId = Integer.parseInt(param.get("postId").toString());
        return JSON.toJSONString(forumsService.removeById(postId));
    }

    //根据postId删除多条记录
    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody Map<String,Object> param) {
        List<Integer> postIds = JSON.parseArray(param.get("postIds").toString(), Integer.class);
        return JSON.toJSONString(forumsService.removeByIds(postIds));
    }
    //根据postId改变帖子状态
    @PostMapping("/changeStatus")
    public String changeStatus(@RequestBody Map<String,Object> param) {
        Integer postId = Integer.parseInt(param.get("postId").toString());
        String status = param.get("status").toString();
        Forums forums = new Forums();
        forums.setPostId(postId);
        forums.setStatus(status);
        return JSON.toJSONString(forumsService.updateById(forums));
    }
}
